const db = require('../db/index')
const bcrypt = require('bcryptjs')
const jwt = require("jsonwebtoken")
const config = require("../config.js")
// 导入处理路径的 path 核心模块
const path = require('path')
const fs = require('fs')

const moment = require('moment');

exports.getcategoryEcharts = (req, res) => {
    let sql = "select product_category,sum(product_all_price) as allprice from product group by product_category";
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        let sql1 = "select * from producttype";
        db.query(sql1, (err, results1) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            let nameList = results1.map(element => {
                return element.name
            });
            const arrayTOobject = Object.fromEntries(
                results.map(({ product_category, allprice }) => [product_category, allprice])
            );
            const valueList = nameList.map(item => arrayTOobject[item] || 0);
            res.send({
                // 状态
                status: 0,
                // 状态描述，判断 err 是 错误对象 还是 字符串
                message: '查询成功',
                data: {
                    nameList: nameList,
                    valueList: valueList
                }
            })
        })
    })
}

exports.getusersEcharts = (req, res) => {
    let sql = "select identity as name, count(*) as value from users group by identity";
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: results
        })
    })
}

exports.getNoticeEcharts = (req, res) => {
    let sql = "select message_level as name, count(*) as value from message where message_category = '公告管理' group by name";
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: results
        })
    })
}

exports.getLoginEcharts = (req, res) => {
    let sql = "select Date(login_time) as date, count(*) as count from login_log group by date";
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);

        const time = new Date().getTime();
        let datas = [];
        for (i = 0; i < 7; i++) {
            datas.push(moment(time - i * 1000 * 60 * 60 * 24).format("YYYY-MM-DD"));
        }
        datas.reverse();

        const arrayTOobject = Object.fromEntries(
            results.map(({ date, count }) => [date, count])
        );

        const valueList = datas.map(item => arrayTOobject[item] || 0);

        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: {
                nameList: datas,
                valueList: valueList
            }
        })
    })
}



exports.getcategoryEchartsOLD = (req, res) => {
    const categoryArr = () => {
        return new Promise(resolve => {
            let sql = "select name from producttype";
            db.query(sql, (err, results) => {
                // 执行 SQL 语句失败
                if (err) return res.cc(err);
                let namelist = [];
                results.forEach(element => {
                    namelist.push(element.name)
                });
                resolve(namelist)
            })
        })
    }
    const getNumber = (product_category) => {
        return new Promise(resolve => {
            let sql = "select product_all_price from product where product_category = ?";
            db.query(sql, product_category, (err, results) => {
                // 执行 SQL 语句失败
                if (err) return res.cc(err);
                let total = 0;
                for (let i = 0; i < results.length; i++) {
                    total += results[i].product_all_price
                }
                resolve(total)
            })
        })
    }

    async function getAll() {
        const category = await categoryArr();
        const price = [];
        for (let i = 0; i < category.length; i++) {
            price[i] = await getNumber(category[i]);
        }
        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: {
                nameList: category,
                valueList: price
            }
        })
    }
    getAll()

}

exports.getusersEchartsOLD = (req, res) => {
    const getNumber = (identity) => {
        return new Promise(resolve => {
            let sql = "select * from users where identity =?";
            db.query(sql, identity, (err, results) => {
                resolve(results.length)
            })
        })
    }
    async function getAll() {
        let data = [
            {
                value: 0,
                name: "用户"
            },
            {
                value: 0,
                name: "产品管理员"
            },
            {
                value: 0,
                name: "用户管理员"
            },
            {
                value: 0,
                name: "消息管理员"
            }
        ]
        for (let i = 0; i < data.length; i++) {
            data[i].value = await getNumber(data[i].name);
        }
        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: data
        })
    }
    getAll()
}

exports.getNoticeEchartsOLD = (req, res) => {
    const getNumber = (message_level) => {
        return new Promise(resolve => {
            let sql = "select * from message where message_level =?";
            db.query(sql, message_level, (err, results) => {
                resolve(results.length)
            })
        })
    }
    async function getAll() {
        let data = [
            {
                value: 0,
                name: "必要"
            },
            {
                value: 0,
                name: "重要"
            },
            {
                value: 0,
                name: "一般"
            }
        ]
        for (let i = 0; i < data.length; i++) {
            data[i].value = await getNumber(data[i].name);
        }
        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: data
        })
    }
    getAll()
}

exports.getLoginEchartsOLD = (req, res) => {
    const getNumber = (data) => {
        return new Promise(resolve => {
            let sql = `select * from login_log where login_time like '%${data}%'`;
            db.query(sql, data, (err, results) => {
                resolve(results.length)
            })
        })
    }
    async function getAll() {
        const time = new Date().getTime();
        let datas = [];
        for (i = 0; i < 7; i++) {
            datas.push(moment(time - i * 1000 * 60 * 60 * 24).format("YYYY-MM-DD"));
        }
        let number = [];
        for (let i = 0; i < datas.length; i++) {
            number[i] = await getNumber(datas[i]);
        }
        res.send({
            // 状态
            status: 0,
            // 状态描述，判断 err 是 错误对象 还是 字符串
            message: '查询成功',
            data: {
                nameList: datas.reverse(),
                valueList: number.reverse()
            }
        })
    }
    getAll()
}
